//
//  CZRefreshControl.m
//  XinLang_OC
//
//  Created by WONG on 16/3/19.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "CZRefreshControl.h"
#import "CZRefreshView.h"

@interface CZRefreshControl ()
/**
 *  自定义刷新的view
 */
@property (nonatomic,strong) CZRefreshView *refreshView;
/**
 *  标记，记录箭头状态
 */
@property(nonatomic,assign)BOOL isUp;

@end

@implementation CZRefreshControl

// 覆盖刷新控件的frame, 当拖动tableView时,frame会不断的改变,tableView往下拖动,frame.origin.y是负数,而且越来越小
- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    
    if (frame.origin.y > 0) {
        // 刷新控件是被tableView挡住的,看不见
        return;
    }
    // 判断刷新控件的刷新状态
    if (self.refreshing) {
        // 刷新控件进入刷新状态,调用refreshView开始旋转
        [self.refreshView startRotation];
    }
    
    // UIRefreshControl默认大小: 宽度等于屏幕的宽度,高度是60
    // 当frame.origin.y > - 60 箭头向下
    // 当frame.origin.y < - 60 箭头向上
    if (frame.origin.y < -60 && !self.isUp) {
        self.isUp = YES;
        // 动画箭头转上去
        [self.refreshView tipIconRoration:self.isUp];
    } else if (frame.origin.y > -60 && self.isUp) {
        self.isUp = false;
        // 动画，箭头转下去
        [self.refreshView tipIconRoration:self.isUp];
    }
}

- (instancetype)init {
    if (self = [super init]) {
        [self prepareUI];
    }
    return self;
}

// 覆盖父类的方法
- (void)endRefreshing {
    [super endRefreshing];
    
    // 让自定义的view停止刷新
    [self.refreshView stopRotation];
}


- (void)prepareUI {
    // 添加控件
    [self addSubview:self.refreshView];
    
    // 添加约束
    [self.refreshView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(self.size);
    }];
}
    
#pragma mark - lazy load

- (CZRefreshView *)refreshView {
    if (!_refreshView) {
        _refreshView = [CZRefreshView refreshView];
    }
    return _refreshView;
}


@end















